#include "JudgementForm.hpp"
#include "ui_JudgementForm.h"
#include "ExamWindow.hpp"

JudgementForm::JudgementForm(RandomQuestion *rdq, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::JudgementForm),
    group(new QButtonGroup(this)),
    randomq(rdq)
{
    ui->setupUi(this);
    ui->plainTextEdit->setReadOnly(true);
    group->addButton(ui->trueBtn);
    group->addButton(ui->falseBtn);
    status=false;
}

JudgementForm::~JudgementForm()
{
    delete ui;
}

extern const QString daanwei_qstr;
const QString zhengque_qstr=QString::fromUtf8("\u6B63\u786E");
const QString cuowu_qstr=QString::fromUtf8("\u9519\u8BEF");

bool JudgementForm::checkCorrection(QString *msg)
{
//    if((ui->trueBtn->isChecked() && answer) || (ui->falseBtn->isChecked() && !answer)){
//        if (msg){
//            //msg->clear();
//            //msg->append(huida_qstr);
//            //msg->append(zhengque_qstr);
//        }
//        return 1;
//    }
//    if (msg){
//        msg->clear();
//        //msg->append(huida_qstr);
//        //msg->append(cuowu_qstr);
//    }
//    return 0;
    if (msg){
        *msg=daanwei_qstr;
        msg->append(answer?zhengque_qstr:cuowu_qstr);
    }
    status=false;
    return true;
}

bool JudgementForm::nextQuestion(QString *msg)
{
    PStaticJudgementQuestion sjq=randomq->randomChooseJudgementQuestion();
    if (!sjq.question){
        if (msg){
            *msg=QString::fromUtf8("\u5224\u65AD\u9898\u5DF2\u5168\u90E8"
                                   "\u7528\u5B8C\uFF0C\u8BF7\u6DFB\u52A0\u65B0\u9898\u5E93");
        }
        return status=false;
    }
    answer=sjq.question->answer;
    ui->plainTextEdit->setPlainText(sjq.question->content);
    bool t=ui->trueBtn->isChecked(),f=ui->falseBtn->isChecked();
    if (t || f){
        group->setExclusive(false);
        if (t){
            ui->trueBtn->setChecked(false);
        }else{
            ui->falseBtn->setChecked(false);
        }
        group->setExclusive(true);
    }
    if (msg){
        *msg=QString::fromUtf8("\u9898\u76EEID\uFF1A%1").arg(sjq.question->id);
    }
    return status=true;
}

void JudgementForm::updateConfig(const ExamConfig *cfg)
{
    ui->trueBtn->setFont(cfg->itemfont);
    ui->falseBtn->setFont(cfg->itemfont);
    ui->plainTextEdit->setFont(cfg->mainfont);
    //ui->label->setFont(cfg->itemfont);
}

void JudgementForm::clearText()
{
    if (!status){
        ui->plainTextEdit->clear();
    }
}
